
#include <BVBoxLayout>
#include <BHBoxLayout>
#include <BPainter>

#include "Signals.h"
#include "MainWindow.h"
#include <BTimer>

BTimer* clock = 0;

MainWindow::MainWindow()
{
	clock = new BTimer();

	initUI();
	initSignals();

	_recorder = new Recorder();
	_speaker = new Speaker();

	_recorder->open();
	_speaker->open();

	_output->addText("audio input device list:");
	for(int i = 0; i < _recorder->deviceNumber(); i++)
	{
		BString name;
		name << '\n'<< _recorder->deviceName(i);
		_output->addText(name);
	}
	_output->addText("\naudio output device list:");
	for(int i = 0; i < _speaker->deviceNumber(); i++)
	{
		BString name;
		name << '\n'<< _speaker->deviceName(i);
		_output->addText(name);
	}
}
MainWindow::~MainWindow()
{
	_recorder->close();
	_speaker->close();
}

void MainWindow::initUI()
{
	WaveViewer* viewer = new WaveViewer(this);
	viewer->setRect(300, 30, 100, 100);

	_output = new BTextEdit(this);
	_output->setRect(10, 100, 200, 300);
	_output->setBorder(5);
	_output->setMargins(5);

	BWidget* toolBar = new BWidget(this);
	toolBar->setSize(900, 36);
	toolBar->setMaxSize(2000, 32);
	toolBar->setSizePolicy(Policy_Expanding);
	{
		_recordButton = new BButton("record");
		_playButton = new BButton("play");
		_stopButton = new BButton("stop");
		_openButton = new BButton("open");
		_saveButton = new BButton("save");

		BHBoxLayout* hlayout = new BHBoxLayout(toolBar);
		hlayout->addWidget(_recordButton);
		hlayout->addWidget(_playButton);
		hlayout->addWidget(_stopButton);
		hlayout->addWidget(_openButton);
		hlayout->addWidget(_saveButton);
	}

	BVBoxLayout* layout = new BVBoxLayout(this);
	layout->addWidget(viewer);
	layout->addWidget(toolBar);
	layout->addWidget(_output);

}
void MainWindow::initSignals()
{
	connect(_recordButton, Signal_Clicked, &MainWindow::procRecord);
	connect(_playButton, Signal_Clicked, &MainWindow::procPlay);
	connect(_stopButton, Signal_Clicked, &MainWindow::procStop);
	connect(_openButton, Signal_Clicked, &MainWindow::procOpen);
	connect(_saveButton, Signal_Clicked, &MainWindow::procSave);

	connect(Signal_Recording, &MainWindow::procRecording);
	connect(Signal_PlayOver, &MainWindow::procPlayOver);
}

void MainWindow::procRecording(BObject* object, const BValue& value)
{
	const BByteArray& buffer = _recorder->buffer();
	BString text("recording byte size: ");
	text << buffer.size();
	_output->setText(text);
}
void MainWindow::procPlayOver(BObject* object, const BValue& value)
{
	_output->addText("\nplay over");
}
void MainWindow::procRecord(BObject* object, const BValue& value)
{
	if(_recorder->recording())
	{
		_recorder->stop();
	}
	else
	{
		_recorder->clear();
		_recorder->start();
	}
}
void MainWindow::procPlay(BObject* object, const BValue& value)
{
	const BByteArray& data = _recorder->buffer();
	_speaker->setBuffer(data);
	_speaker->play();
}
void MainWindow::procStop(BObject* object, const BValue& value)
{

}
void MainWindow::procOpen(BObject* object, const BValue& value)
{

}
void MainWindow::procSave(BObject* object, const BValue& value)
{

}
	
